#!/usr/bin/env bash
baseDir="$(cd `dirname $0`;pwd)"

source ${baseDir}/config


[[ -d "${backDir}" ]] || mkdir -p ${backDir}

while true
do
  nowHour=$(date +%H)
  checkBack="$(ls -l ${backDir} | grep `date +%F`)"

  if [[ "${nowHour}" == "00" ]] && [[ "${checkBack}"x == ""x ]];then
    backTime="$(date +%FT%T%z)"
    mysqldump --host=$mysqlHost \
    --user=$mysqlRootUser \
    --password=$mysqlRootPass \
    --routines --all-databases --single-transaction \
    > ${backDir}/mysql-${backTime}.sql
  else
    sleep 3600
    continue
  fi
done
